package com.vlong.town.service.policy;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vlong.town.entity.policy.PolicyInfo;
import com.vlong.town.mapper.policy.PolicyInfoMapper;

/**
 * 政策业务实现类
 * @author Assassin
 */
@Service
public class PolicyInfoService {

    // Fields

    @Autowired
    private PolicyInfoMapper policyInfoMapper;// 政策Mapper接口

    // Methods

    /**
     * 添加政策
     * @param townId 村镇Id
     * @param title 政策标题
     * @param content 政策内容
     * @param desc 政策简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void add(Long townId, String title, String content, String desc, String thumbnail) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setTownId(townId);
        policyInfo.setState(1);
        policyInfo.setStatus(1);
        policyInfo.setTitle(title);
        policyInfo.setContent(content);
        policyInfo.setDesc(desc);
        policyInfo.setThumbnail(thumbnail);
        Long cuTime = System.currentTimeMillis();
        policyInfo.setCreateTime(cuTime);
        policyInfo.setUpdateTime(cuTime);
        this.policyInfoMapper.insert(policyInfo);
    }

    /**
     * 逻辑删除政策
     * @param policyId 主键值
     */
    @Transactional
    public void delete(Long policyId) {
        this.policyInfoMapper.delete(policyId);
    }

    /**
     * 修改政策
     * @param policyId 政策Id
     * @param title 政策标题
     * @param content 政策内容
     * @param desc 政策简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void update(Long policyId, String title, String content, String desc, String thumbnail) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setPolicyId(policyId);
        policyInfo.setTitle(title);
        policyInfo.setContent(content);
        policyInfo.setDesc(desc);
        policyInfo.setThumbnail(thumbnail);
        policyInfo.setUpdateTime(System.currentTimeMillis());
        this.policyInfoMapper.update(policyInfo);
    }

    /**
     * 主键查询政策
     * @param policyId 政策Id
     * @return
     */
    @Transactional(readOnly = true)
    public PolicyInfo queryById(Long policyId) {
        return this.policyInfoMapper.selectById(policyId);
    }

    /**
     * 查询政策
     * @param title 政策标题
     * @param content 政策内容
     * @param desc 政策简介
     * @return List<PolicyInfo>
     */
    @Transactional(readOnly = true)
    public List<PolicyInfo> queryList(String title, String content, String desc) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setTitle(title);
        policyInfo.setContent(content);
        policyInfo.setDesc(desc);
        return this.policyInfoMapper.selectList(policyInfo);
    }

}
